# Copyright (c) Zhirnov Andrey. For more information see 'LICENSE'

if (TARGET "GLSLang-lib")
	if ( (TARGET "Vulkan-lib") OR (EXISTS "${AE_EXTERNAL_SHARED_PATH}/Vulkan/vulkan/vulkan.h") )
		set( HAS_VULKAN_HEADERS ON )
	endif()

	set( GRAPHICS_DIR "${MAIN_SOURCE_DIR}/engine/src/graphics" )
	set( GRAPHICS_HPP_SRC
		"${GRAPHICS_DIR}/Scripting/GraphicsBindings.h"
		"${GRAPHICS_DIR}/Public/FeatureSet.h"
		"${GRAPHICS_DIR}/Public/RenderState.h"
		"${GRAPHICS_DIR}/Public/PipelineDesc.h"
		"${GRAPHICS_DIR}/Public/SamplerDesc.h"
		"${GRAPHICS_DIR}/Private/EnumUtils.h"
		"${GRAPHICS_DIR}/Private/EnumToString.h"
		"${GRAPHICS_DIR}/Public/IDs.h"
		"${GRAPHICS_DIR}/Vulkan/VEnumCast.h" )
	if (NOT (DEFINED ENGINE_LIBS_PATH))
		set( GRAPHICS_CPP_SRC
			"${GRAPHICS_DIR}/Scripting/GraphicsBindings.cpp"
			"${GRAPHICS_DIR}/Private/FeatureSet.cpp"
			"${GRAPHICS_DIR}/Private/RenderState.cpp"
			"${GRAPHICS_DIR}/Private/EnumUtils.cpp"
			"${GRAPHICS_DIR}/Private/ImageDesc.cpp" )
		if (${HAS_VULKAN_HEADERS})
			set( GRAPHICS_CPP_SRC ${GRAPHICS_CPP_SRC} "${GRAPHICS_DIR}/Vulkan/VEnumCast.cpp" )
		endif()
	endif()
	file( GLOB_RECURSE SOURCES "*.*" )

	add_library( "PipelineCompiler" STATIC ${SOURCES} ${GRAPHICS_HPP_SRC} ${GRAPHICS_CPP_SRC} ${PREBUILD_CPP_FILE} )
	source_group( TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES} )
	source_group( "external/graphics" FILES ${GRAPHICS_HPP_SRC} ${GRAPHICS_CPP_SRC} )

	target_link_libraries( "PipelineCompiler" PUBLIC "Serializing" "Scripting" "Threading" )
	target_compile_definitions( "PipelineCompiler" PRIVATE "AE_BUILD_PIPELINE_COMPILER" "AE_TEST_PIPELINE_COMPILER" )
	target_include_directories( "PipelineCompiler" PUBLIC "." )

	if ( WIN32 AND (TARGET "SPIRV-Cross-lib") )
		target_link_libraries( "PipelineCompiler" PUBLIC "SPIRV-Cross-lib" )
		set( METAL_TOOLS "${AE_EXTERNAL_BIN_PATH}/external/win-x64/MetalTools/bin" )
		if ( NOT EXISTS "${METAL_TOOLS}" )
			message( STATUS "Metal tools '${METAL_TOOLS}' is not exists" )
		endif()
		if ( EXISTS "${METAL_TOOLS}" )
			target_compile_definitions( "PipelineCompiler" PUBLIC "AE_METAL_TOOLS=\"${METAL_TOOLS}\"" )
		endif()
	endif()

	if (${HAS_VULKAN_HEADERS})
		target_include_directories( "PipelineCompiler" PRIVATE "${AE_EXTERNAL_SHARED_PATH}/Vulkan" )
		target_compile_definitions( "PipelineCompiler" PRIVATE "AE_ENABLE_VULKAN" )
	endif()

	target_link_libraries( "PipelineCompiler" PUBLIC "GLSLang-lib" )

	if (TARGET "ShaderTrace")
		target_link_libraries( "PipelineCompiler" PUBLIC "ShaderTrace" )
	endif()

	# must be same as in 'Graphics' module
	if (DEFINED AE_GRAPHICS_STRONG_VALIDATION)
		if (${AE_GRAPHICS_STRONG_VALIDATION})
			target_compile_definitions( "PipelineCompiler" PRIVATE AE_GRAPHICS_STRONG_VALIDATION=1 )
		else()
			target_compile_definitions( "PipelineCompiler" PRIVATE AE_GRAPHICS_STRONG_VALIDATION=0 )
		endif()
	else()
		message( FATAL_ERROR "AE_GRAPHICS_STRONG_VALIDATION is not defined" )
	endif()

	EnablePCH( "PipelineCompiler" )
	EnablePrebuild( "PipelineCompiler" )

	add_library( "PipelineCompiler-shared" SHARED
		"PipelineCompiler.h"
		"Lib/Export.cpp"
		"Lib/PipelineCompiler.def"
	)
	target_link_libraries( "PipelineCompiler-shared" PUBLIC "PipelineCompiler" )

	set_property( TARGET "PipelineCompiler" PROPERTY FOLDER "Engine/ResPack" )
	set_property( TARGET "PipelineCompiler-shared" PROPERTY FOLDER "Engine/ResPack" )

	# generated cpp file used as dependency to trigger project rebuild
	set( PipelineCompiler.trigger "${CMAKE_CURRENT_SOURCE_DIR}/PipelineCompiler.trigger.cpp" CACHE INTERNAL "" FORCE )
	add_custom_command( TARGET "PipelineCompiler-shared" POST_BUILD COMMAND ${CMAKE_COMMAND} -E touch "${PipelineCompiler.trigger}" )
	set_source_files_properties( "${PipelineCompiler.trigger}" PROPERTIES GENERATED TRUE )
endif()
